<?php
namespace app\system\controller;

use app\common\model\AreaModel;
use app\common\model\AuthRuleModel;
use app\common\model\CmsCategoryModel;
use app\common\model\MoudleModel;
use app\common\model\ConfigModel;
use app\common\model\DictModel;
use app\common\model\UserModel;
use think\Cookie;
use think\Session;

class IndexController extends BaseController
{
    /**
     * 初始化
     */
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     * @return mixed
     */
    public function index()
    {
        $userModel = new UserModel();
        $userinfo  = $userModel->get($this->getUid());
        $title     = '控制台';
        $ruleList  = $userModel->getMyRuleList($this->getGroupIds(), $this->getUtype());
        $this->assign('title', $title);
        $this->assign('userinfo', $userinfo);
        $this->assign('group_ids', $this->getGroupIds());
        $this->assign('ruleList', $ruleList);
        return $this->fetch();
    }

    /**
     * 右侧菜单
     */
    public function getMenu(){
        $userModel = new UserModel();
        $groupid = $this->request->param('groupid');
        $ruleid = $this->request->param('ruleid');
        $ruleList  = $userModel->getMyRuleList($groupid, $this->getUtype());
        $this->assign('ruleList',$ruleList[$ruleid]['children']);
        echo $this->fetch();
    }

    /**
     * 主窗口
     */
    public function main()
    {
        return $this->fetch();
    }

    /**
     * 初始化数据
     */
    public function initCache()
    {
        if ($this->getUtype() == 'system') {
            $areaModel        = new AreaModel();
            $cmsMoudleModel   = new MoudleModel();
            $dictModel        = new DictModel();
            $configModel        = new ConfigModel();
            $cmsCategoryModel   = new CmsCategoryModel();
            $areaModel->updateCache(); //更新地区缓存
            $arr[] = "地区缓存[area]更新完毕！";
            $cmsMoudleModel->updateTableCache(); //更新cms模型缓存
            $arr[] = "模型缓存[cms_moudle]、字段缓存[cms_field]、推荐位缓存[cms_flag]更新完毕！";
            $dictModel->updateCache(); //更新字典缓存
            $arr[] = "字典缓存[dict]更新完毕！";
            $configModel->updateCache(); //更新配置缓存
            $arr[] = "站点配置缓存[webconfig]更新完毕！";
            $cmsCategoryModel->updateCache(); //更新cms栏目缓存
            $arr[] = "cms栏目缓存[cms_category}]更新完毕！";
        }
        $authRuleModel      = new AuthRuleModel();
        $authRuleModel->updateCache($this->getUtype()); //更新权限规则缓存
        $arr[] = "规则缓存[auth_rule_{$this->getUtype()}]更新完毕！";
        $this->assign('arr', $arr);
        return $this->fetch();
    }

    /**
     * 退出登录
     */
    public function exitLogin()
    {
        Cookie::delete($this->getUtype().'Des');
        Session::delete('group_ids');
        $this->success('退出登录成功！', url('Public/login'));
    }

}
